package zhu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Register extends JFrame {
    private JTextField studentIdField;
    private JTextField nameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JTextField secretQuestionField;
    private JPasswordField secretAnswerField;
    private JComboBox<String> roleComboBox;

    public Register() {
        setTitle("注册");
        setSize(400, 350);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(8, 2));

        add(new JLabel("学号："));
        studentIdField = new JTextField();
        add(studentIdField);

        add(new JLabel("姓名："));
        nameField = new JTextField();
        add(nameField);

        add(new JLabel("密码："));
        passwordField = new JPasswordField();
        add(passwordField);

        add(new JLabel("确认密码："));
        confirmPasswordField = new JPasswordField();
        add(confirmPasswordField);

        add(new JLabel("密保问题："));
        secretQuestionField = new JTextField();
        add(secretQuestionField);

        add(new JLabel("密保答案："));
        secretAnswerField = new JPasswordField();
        add(secretAnswerField);

        add(new JLabel("角色："));
        roleComboBox = new JComboBox<>(new String[]{"学生", "管理员"});
        add(roleComboBox);
        JButton registerButton = new JButton("注册");
        add(registerButton);

        // 登录按钮
        JButton loginButton = new JButton("登录");
        add(loginButton);

        // 为注册按钮添加事件监听器
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                registerUser();
            }
        });

        // 为登录按钮添加事件监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showLoginWindow();
            }
        });
    }
    private void showLoginWindow() {
        Login login = new Login();
        login.setVisible(true);
    }
    private void registerUser() {
        String studentId = studentIdField.getText();
        String name = nameField.getText();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String secretQuestion = secretQuestionField.getText();
        String secretAnswer = new String(secretAnswerField.getPassword());
        String role = (String) roleComboBox.getSelectedItem();

        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "密码不匹配！");
            return;
        }

        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement("INSERT INTO users (student_id, name, password, role, secret_question, secret_answer) VALUES (?, ?, ?, ?, ?, ?)")) {

            stmt.setString(1, studentId);
            stmt.setString(2, name);
            stmt.setString(3, password);
            stmt.setString(4, role);
            stmt.setString(5, secretQuestion);
            stmt.setString(6, secretAnswer);

            int rowsInserted = stmt.executeUpdate();
            if (rowsInserted > 0) {
                JOptionPane.showMessageDialog(this, "用户注册成功！");
                dispose(); // 关闭注册窗口
                showLoginWindow(); // 显示登录窗口
            } else {
                JOptionPane.showMessageDialog(this, "注册用户失败！");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "错误：" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Register().setVisible(true));
    }
}

